package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override

    //添加检查组
    public int add(CheckGroup checkGroup, Integer[] checkitemIds) {
        int codeCount = checkGroupDao.findCountByCode(checkGroup);
        if (codeCount > 0) {
            throw new RuntimeException("该检查组编码已存在，请勿重复添加");
        }

        int nameCount = checkGroupDao.findCountByName(checkGroup);
        if (nameCount > 0) {
            throw new RuntimeException("该检查组名称已存在，请勿重复添加");
        }

        int count = checkGroupDao.add(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        System.out.println("checkGroupId = " + checkGroupId);
        if (checkitemIds != null && checkitemIds.length > 0) {
            Map<String, Integer> map = new HashMap<>();
            map.put("checkgroup_id", checkGroupId);
            for (Integer checkitemId : checkitemIds) {
                map.put("checkitem_id", checkitemId);
//                map.put("checkgroup_id",checkGroupId);
                checkGroupDao.addCheckItemAndCheckGroup(map);
            }
        }

        return count;
    }

    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        Integer currentPage = queryPageBean.getCurrentPage();
//        if (currentPage==null|| currentPage < 1){
//            currentPage =1;
//        }
        Integer pageSize = queryPageBean.getPageSize();
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }
        String queryString = queryPageBean.getQueryString();
        if (queryString == null || queryString.trim().equals("")) {
            queryString = "";
        }
        PageHelper.startPage(currentPage, pageSize);

        Page<CheckGroup> page = checkGroupDao.findByCondition("%" + queryString.trim() + "%");

        return new PageResult(page.getTotal(), page.getResult());

    }

    //根据id查询检查组
    @Override
    public CheckGroup findById(Integer id) {

        CheckGroup checkGroup = checkGroupDao.findById(id);
        List<Integer> checkitemIds = checkGroupDao.findCheckitemIds(id);

        checkGroup.setCheckitemIds(checkitemIds);
        return checkGroup;
    }


    //编辑检查组
    @Override
    public int edit(CheckGroup checkGroup, Integer[] checkitemIds) {


        //删除之前关联的检查项
        Integer checkGroupId = checkGroup.getId();
        checkGroupDao.deleteCheckItemAndCheckGroupById(checkGroupId);
        //在中间表中插入关联的新检查项

        System.out.println("checkGroupId = " + checkGroupId);
        if (checkitemIds != null && checkitemIds.length > 0) {
            Map<String, Integer> map = new HashMap<>();
            map.put("checkgroup_id", checkGroupId);
            for (Integer checkitemId : checkitemIds) {
                map.put("checkitem_id", checkitemId);
//                map.put("checkgroup_id",checkGroupId);
                checkGroupDao.addCheckItemAndCheckGroup(map);
            }
        }
        //编辑检查组
        int count = checkGroupDao.edit(checkGroup);
        return count;
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }

    @Override
    public int deleteById(Integer id) {
        int count = 0;
        //首先删除关联项
        if (id!=null){
            checkGroupDao.deleteCheckItemAndCheckGroupById(id);
            //再删除检查组
            count = checkGroupDao.deleteCheckGroupById(id);
        }
        return count;
    }

}
